package com.service.imp;

import com.bean.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//业务逻辑实现类
public class UserServiceImp implements UserService{
    @Override
    public String selectByName(User u) {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory fy = new SqlSessionFactoryBuilder().build(is);
        SqlSession sq = fy.openSession();
        UserMapper mp = sq.getMapper(UserMapper.class);
        User user = mp.selectByName(u.getUsername());
//        判断账号是否为空
        if (user==null){

        }
        if (user.getPassword().equals(u.getPassword())){
            return "登录成功";
        }
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }return "账号或密码不存在";
    }


}
